#include "../include/header.h"

/**
 * 守护进程
*/
int main(int argc, char* argv[]){
    char buf[] = "我是守护进程\n";

    pid_t pid = fork();
    ERROR_CHECK(pid, -1, "fork");

    if(pid > 0){
        // father
        exit(0);
    }else{
        setsid();
        chdir("/tmp");
        umask(0);

        for(int i = 0; i < 1024; i++){
            close(i);
        }

        int fd = open("server.log", O_CREAT | O_WRONLY | O_TRUNC, 0666);
        ERROR_CHECK(fd, -1, "open");

        while(1){
            write(fd, buf, sizeof buf);
            sleep(5);
        }
        close(fd);
    }
    return 0 ;
}